;;; eip_matlab_mod.el --- 

;; Copyright (C) 2008  ShineGood

;; Author: ShineGood <ShineGood@bj1860.net>
;; Keywords: 

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; 

;;; Code:
(require 'matlab)
(require 'matlab-eei)

(autoload 'matlab-eei-connect "matlab-eei"
  "Connects Emacs to MATLAB's external editor interface.")
(setq auto-mode-alist (cons '("\\.[mM]$" . matlab-mode) auto-mode-alist))
(autoload 'matlab-mode "matlab" "Enter MATLAB mode." t)
(autoload 'matlab-shell "matlab" "Interactive MATLAB mode." t)

(setq matlab-indent-function t) ; if you want function bodies indented
(setq matlab-verify-on-save-flag nil) ; turn off auto-verify on save

(setq matlab-shell-command-switches '("-nodesktop -nosplash"))

(setq matlab-indent-level 4)
(setq matlab-shell-ask-MATLAB-for-completions t)

(defun viper-intercept-ESC-key ()
  "Function that implements ESC key in Viper emulation of Vi."
  (interactive)
  (let ((cmd (or (key-binding (viper-envelop-ESC-key))
		 '(lambda () (interactive) (error "")))))

    ;; call the actual function to execute ESC (if no other symbols followed)
    ;; or the key bound to the ESC sequence (if the sequence was issued
    ;; with very short delay between characters).
    (if (eq cmd 'viper-intercept-ESC-key)
	(setq cmd
	      (cond ((eq viper-current-state 'vi-state)
		     'viper-ESC)
		    ((eq viper-current-state 'insert-state)
		     'viper-exit-insert-state)
		    ((eq viper-current-state 'replace-state)
		     'viper-replace-state-exit-cmd)
		    (t 'viper-change-state-to-vi)
		    )))
    (call-interactively cmd)
    (setq this-command cmd)))


(defun eip-matlab-mode ()
  (linum-mode)
  (local-set-key "\t" 'my-indent-or-complete)
  (imenu-add-to-menubar "Find")
  (setq matlab-functions-have-end t)
  ;(nconc hippie-expand-try-functions-list '(matlab-complete-symbol))
  (if viper-mode
      (progn
	(viper-add-local-keys 'insert-state '(("\r" . matlab-linefeed)
										  ([(meta \;)] . comment-dwim)))
	(viper-add-local-keys 'vi-state '(([(meta \;)] . comment-dwim))))
    (define-key matlab-mode-map "\r" 'matlab-linefeed)
    (define-key matlab-mode-map "M-\;" 'comment-dwim))
  )
  
(add-hook 'matlab-mode-hook 'eip-matlab-mode)

(defun my-matlab-shell-mode-hook ()
  (setenv "TERM" "xterm")
  (setq gud-matlab-marker-regexp-2
	(concat "^<a href=\"\\([^\":,]+\\)\\([^\":,]+\\)\\([^\":,]+\\)\">" "")) 
  (if (not viper-mode)
      (progn
	(define-key matlab-shell-mode-map [(home)] 'beginning-of-line)
	(if cua-mode
	    (put 'beginning-of-line 'CUA 'move)))
    (viper-add-local-keys 'insert-state '(("\r" . viper-exec-key-in-emacs)
					  ([(tab)] . my-indent-or-complete)
					  ("\t" . my-indent-or-complete)
					  ("\M-/" . matlab-shell-tab)
					  ([(home)] . viper-beginning-of-line)
					  ([(backspace)] . matlab-shell-delete-backwards-no-prompt)))
;    (viper-add-local-keys 'insert-state '(([(up)] . comint-previous-matching-input-from-input)))
    )
  ;(nconc hippie-expand-try-functions-list '(matlab-complete-symbol))
  )
(add-hook 'matlab-shell-mode-hook 'my-matlab-shell-mode-hook)

;;; eip_matlab_mod.el ends here

